#include <lpc23xx.h>
#include <RTL.h>
#include "LCD.h"

OS_TID  id1,id2;

__task void task1(void);
__task void task2(void);
os_mbx_declare(MsgBox,16);
typedef struct{
unsigned int ADresult;
}MESSAGE;

unsigned int mpool[16*sizeof(MESSAGE)/4+3];
_declare_box (mpool,sizeof(MESSAGE),16);

int main(void)
{
lcd_init();					   
lcd_clear();				  
set_cursor(2,0);			   
lcd_print("2task Example");
FIO2DIR=0x000000FF;
PCONP |= 0x00001000;  
PINSEL1 |= 0x00004000;	

_init_box(mpool,sizeof(mpool),sizeof(MESSAGE));
os_sys_init_prio (task1,0x01);
	 
}
__task void task1(void){
   
   MESSAGE *mptr;
   int result;
   //AD0CR = 0x00200D00;
   //AD0CR |= 0x01000000;
   os_mbx_init (MsgBox, sizeof(MsgBox));
   id1 = os_tsk_self ();
   id2 = os_tsk_create (task2,0x01);
	while(1)
	{
	mptr=_alloc_box(mpool);
	os_evt_set (0x0004, id2);
	os_evt_wait_or (0x0004, 0xffff);

	//if((AD0GDR &0x80000000))
       // {
        //    result = ((AD0GDR >>8)&0xFF);	  
         //   FIO2SET=result;
			mptr->ADresult=0x00000055;
			os_mbx_send(MsgBox,mptr,0xffff);
			FIO2SET=0x00000066;
		//	AD0CR |= 0x01000000;
			
        //}
	
	}
	
	
	

}
__task void task2(void){
	MESSAGE *rptr;
	while(1)
	{
	 os_evt_wait_or (0x0004, 0xffff);
	 os_mbx_wait(MsgBox,(void**)&rptr,0xffff);
	 //FIO2CLR=0x000000FF;
	 FIO2SET=rptr->ADresult;
	 _free_box(mpool,rptr);

	os_dly_wait (20);
	os_evt_set (0x0004, id1);
	

	}

}



